#include "UapStateMediaTransmission.h"
#include "UapStateMap.h"
#include "MgwGapProxy.h"
#include "MediaLibrary.h"
#include "Trace.h"

namespace tsc
{
namespace sgw
{

using namespace tsc::common;

/*******************************************************************************
 *
 * Constructor
 *
 ******************************************************************************/
UapStateMediaTransmission::UapStateMediaTransmission() :
	UapStateBase("MediaTransmission")
{

} // UapStateMediaTransmission::UapStateMediaTransmission

/*******************************************************************************
 *
 * Destructor
 *
 ******************************************************************************/
UapStateMediaTransmission::~UapStateMediaTransmission()
{

} // UapStateMediaTransmission::~UapStateMediaTransmission


UapStateMediaTransmission* UapStateMediaTransmission::instance()
{
	static UapStateMediaTransmission mInstance;
	return &mInstance;
}

//===================================================================
void
UapStateMediaTransmission::handleEventUapConnectionReleaseReq(
    UapGenerator* uapGen,
    UapEventConnectionReleaseReq* event,
    EventData*& eventData)
{
    TRACE(3, STR("Received UapConnectionRelease during media transmission. Sending "
                 "GapMediaBearerRelease to MGW, connId=0x%x",
                 uapGen->getConnectionId()));

    if(!MgwGapProxy::instance().sendGapMediaBearerRelease(
            uapGen->getConnectionId()))
    {
        TRACE(1, STR("Failed to send GapMediaBearerRelease, connId=0x%x - "
                     "anyway deleting UapGenerator",
                     uapGen->getConnectionId()));
    }

    delete uapGen;
    uapGen = 0;

    EventData::deleteEvent(eventData);
}

//===================================================================
void
UapStateMediaTransmission::handleEventGapMediaTransmissionFinishedInd(
    UapGenerator* uapGen,
    UapEventGapMediaTransmissionFinishedInd* event,
    EventData*& eventData)
{
    TRACE(3, STR("Received MediaTransmissionFinishedInd, connId=0x%x",
                 uapGen->getConnectionId()));

    uapGen->setMediaItemId(common::MediaLibrary::INVALID_MEDIA_ITEM_ID);
    uapGen->setServerMediaPort(0);
    uapGen->setServerMediaAddress(0);

    uapGen->changeState(UapStateMap::mStateConnected);

    EventData::deleteEvent(eventData);
}

} // namespace sgw
} // namespace tsc
